/**
 * 
 */
package persistence.DAO.SQL.entities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import metier.Article;
import metier.Bean;
import metier.Pronostic;
import persistence.ConnectionManager;
import persistence.DAO.DAO;
import persistence.DAO.DAOSQL;

/**
 * @author hozakan
 *
 */
public class DAOSQLPronostic extends DAOSQL {

	private static DAO instance;
	private static final Logger logger = Logger.getLogger(DAOSQLPronostic.class);
		
	public static DAO getInstance() {
		if (instance == null)
			instance = new DAOSQLPronostic();
		return instance;
	}
	
	private DAOSQLPronostic() {
		try {
			insert = prepareInsert();
			update = prepareUpdate();
			delete = prepareDelete();
			select = prepareSelect();
		    selectAll = prepareSelectAll();	
		} catch (Exception e) {
			logger.error("Erreur lors de la création des PreparedStatement");
		}
	}


	private boolean create(Bean bean)
	{
		boolean retour = false;
		Pronostic prono = (Pronostic) bean;
		try {
			ConnectionManager.getInstance().getConnection();
			insert.clearParameters();
			insert.setString(1, prono.getIdUtilisateur());
			insert.setString(2, prono.getIdEvenement());
			insert.setString(3, prono.getResultat());
			insert.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			logger.error("Erreur lors de la création d'un pronostique");
		} catch (ClassNotFoundException e) {
			logger.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}

	private boolean update(Bean bean) {
		boolean retour = false;
		Pronostic prono = (Pronostic) bean;
		try {
			ConnectionManager.getInstance().getConnection();
			update.clearParameters();
			update.setString(1, prono.getIdUtilisateur());
			update.setString(2, prono.getIdEvenement());
			update.setString(3, prono.getResultat());
			update.setInt(4, prono.getId());
			update.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			logger.error("Erreur lors de la modification d'un pronostique");
		} catch (ClassNotFoundException e) {
			logger.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}
	
	
	@Override
	public boolean effacer(Bean bean) {
		boolean retour = false;
		try {
			ConnectionManager.getInstance().getConnection();
			delete.clearParameters();
			delete.setInt(1, bean.getId());
			delete.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			logger.error("Erreur lors de la suppression d'un pronostique");
		} catch (ClassNotFoundException e) {
			logger.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}

	@Override
	public boolean enregistrer(Bean bean) {
		boolean retour = false;
		if (bean.getId() == 0) {
			retour = create(bean);
		} else {
			retour = update(bean);
		}
		return retour;
	}

	@Override
	protected PreparedStatement prepareDelete() throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("DELETE FROM pronostic WHERE idPronostic=?");
		return ps;
	}

	@Override
	protected PreparedStatement prepareInsert()  throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("INSERT INTO pronostic " +
				"(idUtilisateur, idEvenement, resultat) VALUES (?, ?, ?)");
		return ps;
	}

	@Override
	protected PreparedStatement prepareUpdate()  throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("UPDATE pronostic SET " +
				"idUtilisateur=?, idEvenement=?, resultat=? WHERE idPronostic=?");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareSelect()
	 */
	@Override
	protected PreparedStatement prepareSelect() throws SQLException,
			ClassNotFoundException
	{
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("SELECT * FROM pronostic WHERE idPronostic=?");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareSelectAll()
	 */
	@Override
	protected PreparedStatement prepareSelectAll() throws SQLException,
			ClassNotFoundException
	{
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("SELECT * FROM pronostic ");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @see persistence.DAO.DAO#getBeans()
	 */
	@Override
	public List<Bean> getBeans()
	{
		ResultSet rs = null;
		List<Bean> beans = new ArrayList<Bean>();
		try {
			ConnectionManager.getInstance().getConnection();
			rs = selectAll.executeQuery();
			while (rs.next()) {
				Pronostic prono = new Pronostic();
				prono.setId(Integer.parseInt(rs.getString("idPronostic")));
				prono.setIdUtilisateur((rs.getString("idUtilisateur")));
				prono.setIdEvenement(rs.getString("idEvenement"));
				prono.setResultat(rs.getString("resultat"));
				beans.add(prono);
			}
		} catch (SQLException e) {
			logger.error("Erreur lors de la préparation du PreparedStatement");
		} catch (ClassNotFoundException e) {
			logger.error("impossible d'obtenir la connexion");
		}
		return beans;
	}

	/*__________________________________________________________*/
	/**
	 * @param id
	 * @return
	 * @see persistence.DAO.DAO#selectionner(int)
	 */
	@Override
	public Bean selectionner(int id)
	{
		Bean bean = null;
		try {
			ConnectionManager.getInstance().getConnection();
			select.clearParameters();
			select.setInt(1, id);
			ResultSet rs = select.executeQuery();
			while (rs.next()) {
				Pronostic prono = new Pronostic();
				prono.setId(Integer.parseInt(rs.getString("idPronostic")));
				prono.setIdUtilisateur((rs.getString("idUtilisateur")));
				prono.setIdEvenement(rs.getString("idEvenement"));
				prono.setResultat(rs.getString("resultat"));
				bean = prono;
			}
		} catch (SQLException e) {
			logger.error("Erreur lors de la préparation du PreparedStatement");
		} catch (ClassNotFoundException e) {
			logger.error("impossible d'obtenir la connexion");
		}
		return bean;
	}
	
}
